package server.mapper;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Host
 * @Description
 * @Author wugz
 * @Date 2020/3/31 13:32
 * @Version 1.0
 */
public final class Host {

    private String appBase;

    private Map<String,Context> contextMap;


    public Host(String appBase) throws Exception{
        setAppBase(appBase);
        try {
            /**获取文件流  */
            DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(appBase));
            /** 此处每个path 都代表一个应用 */
            contextMap = new HashMap<>();
            for(Path path : directoryStream) {
                String contextName = path.getFileName().toString();
                contextMap.put(contextName,new Context(contextName,path));
            }
            directoryStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public Map<String, Context> getContextMap() {
        return contextMap;
    }

    public void setContextMap(Map<String, Context> contextMap) {
        this.contextMap = contextMap;
    }
}
